package com.xgkj.diyiketang.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.util.ToastUtil;

public class ZhunyunSchoolActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout linearLayout;
    private PopupWindow payPopwindow;
    private PopupWindow xieyiPopwindow;
    private ImageView close;
    private LinearLayout jibie_pay, yonghuxieyi;
    private TextView jibie, jianjie, pay_num, pay_btn, zhuanyun_back,title_bar;
    private EditText yaoqingma;
    private int paytaskid = -1;
    private String pay_money = "";
    private String orderNo;
    private String jump_jibie = "";
    private String type;
    private String vipcode;
    private RelativeLayout yuanzhang_rl, zhuren_rl, daoshi_rl, xueyuan_rl;
    private ImageView yuanzhang, daoshi, zhuren, xueyuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhunyun_school);
        title_bar = (TextView) findViewById(R.id.title_bar);
        jibie_pay = (LinearLayout) findViewById(R.id.jibie_pay);
        yuanzhang = (ImageView) findViewById(R.id.yuzhang_rl);
        zhuren = (ImageView) findViewById(R.id.zhuren_rl);
        daoshi = (ImageView) findViewById(R.id.daoshi_rl);
        xueyuan = (ImageView) findViewById(R.id.xueyuan_rl);
        zhuanyun_back = (TextView) findViewById(R.id.zhuanyun_back);
        zhuanyun_back.setOnClickListener(this);
        yuanzhang.setOnClickListener(this);
        zhuren.setOnClickListener(this);
        daoshi.setOnClickListener(this);
        xueyuan.setOnClickListener(this);
        if (getIntent().getExtras() != null) {
            type = getIntent().getExtras().getString("type");
            if (type.equals("zhuanyun")){
                title_bar.setText("转运学院");
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_yz"))) {
//                yuanzhang.setBackgroundResource(R.drawable.);
                    if (getIntent().getExtras().getString("zhuanyun_yz").equals("院长"))
                        yuanzhang.setImageResource(R.drawable.zedang_x);
                    yuanzhang.setEnabled(false);
                }
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_zr"))) {
                    if (getIntent().getExtras().getString("zhuanyun_zr").equals("主任"))
                        zhuren.setImageResource(R.drawable.zedang_x);
                    zhuren.setEnabled(false);
                }
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_ds"))) {
                    if (getIntent().getExtras().getString("zhuanyun_ds").equals("导师"))
                        daoshi.setImageResource(R.drawable.zedang_x);
                    daoshi.setEnabled(false);
                }
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_xy"))) {
                    if (getIntent().getExtras().getString("zhuanyun_xy").toString().equals("学员"))
                        xueyuan.setImageResource(R.drawable.zedang_x);
                    xueyuan.setEnabled(false);
                }
            }else if (type.equals("parent")){
                title_bar.setText("父母大学");
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_yz"))) {
//                yuanzhang.setBackgroundResource(R.drawable.);
                    if (getIntent().getExtras().getString("zhuanyun_yz").equals("院长"))
                        yuanzhang.setImageResource(R.drawable.zedang_x);
                    yuanzhang.setEnabled(false);
                }
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_zr"))) {
                    if (getIntent().getExtras().getString("zhuanyun_zr").equals("主任"))
                        zhuren.setImageResource(R.drawable.zedang_x);
                    zhuren.setEnabled(false);
                }
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_ds"))) {
                    if (getIntent().getExtras().getString("zhuanyun_ds").equals("导师"))
                        daoshi.setImageResource(R.drawable.zedang_x);
                    daoshi.setEnabled(false);
                }
                if (!TextUtils.isEmpty(getIntent().getExtras().getString("zhuanyun_xy"))) {
                    if (getIntent().getExtras().getString("zhuanyun_xy").toString().equals("学员"))
                        xueyuan.setImageResource(R.drawable.zedang_x);
                    xueyuan.setEnabled(false);
                }
            }

        }
    }

    //    private void getPopwindow(){
//        View inflate = LayoutInflater.from(this).inflate(R.layout.item_goumai_pop, null);
//        WindowManager wm = this.getWindowManager();
//        int width = wm.getDefaultDisplay().getWidth();
//        int height = wm.getDefaultDisplay().getHeight();
//        qidaiPop = new PopupWindow(inflate,width-60 ,ViewGroup.LayoutParams.WRAP_CONTENT,true);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f; //0.0-1.0
//        getWindow().setAttributes(lp);
//        ColorDrawable dw = new ColorDrawable(0x60000000);
//        qidaiPop.setBackgroundDrawable(dw);
//        qidaiPop.setTouchable(true);
//        qidaiPop.setOutsideTouchable(false);
//        qidaiPop.setFocusable(true);
//        qidaiPop.showAtLocation(inflate, Gravity.CENTER,0,0);
//    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhuanyun_back:
                finish();
                break;
            case R.id.yuzhang_rl:
                getPopwindow("yuzhang_rl");
                break;
            case R.id.zhuren_rl:
                getPopwindow("zhuren_rl");
                break;
            case R.id.daoshi_rl:
                getPopwindow("daoshi_rl");
                break;
            case R.id.xueyuan_rl:
                getPopwindow("xueyuan_rl");
                break;
            case R.id.zhuanyun_pay_now:
                if (agree_dis) {
                    String s = yaoqingma.getText().toString();
                    Intent intent = new Intent(this, RechargeActivity.class);
                    intent.putExtra("type", type);
                    intent.putExtra("jibie", jump_jibie);
                    intent.putExtra("yaoqingma", s);
                    startActivity(intent);
                } else {
                    ToastUtil.showMessage(this, "请先同意用户购买协议");
                    return;
                }

//                zhifu(s);
                break;
            case R.id.zhuanyun_close:
                payPopwindow.dismiss();
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f; //0.0-1.0
                getWindow().setAttributes(lp);
                break;
            case R.id.yonghuxieyi_ll:
                huiyuanxieyiPop();
                break;
            case R.id.yonghuxieyi_back:
                if (xieyiPopwindow != null) {
                    xieyiPopwindow.dismiss();
                }
                break;
            case R.id.yonghuxieyi_agree:
                if (flag) {
                    flag = false;
                    agree_dis = false;
                    yonghu_agree.setImageResource(R.drawable.disagree);
                } else {
                    flag = true;
                    agree_dis = true;
                    yonghu_agree.setImageResource(R.drawable.agree);
                }
                break;
        }

    }

    private boolean agree_dis = true;
    private boolean flag = true;
    private TextView back;
    private ImageView yonghu_agree;

    private void huiyuanxieyiPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.huiyuanxieyi, null);
        back = (TextView) view.findViewById(R.id.yonghuxieyi_back);
        yonghu_agree = (ImageView) view.findViewById(R.id.yonghuxieyi_agree);
        back.setOnClickListener(this);
        yonghu_agree.setOnClickListener(this);
        xieyiPopwindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        xieyiPopwindow.setBackgroundDrawable(new ColorDrawable());
        xieyiPopwindow.setFocusable(true);
        xieyiPopwindow.setOutsideTouchable(true);
        xieyiPopwindow.showAtLocation(jibie_pay, Gravity.CENTER, 0, 0);
        xieyiPopwindow.update();
    }

    private void getPopwindow(String type) {
        View view = LayoutInflater.from(this).inflate(R.layout.pay_popwindow, null);
        linearLayout = (RelativeLayout) view.findViewById(R.id.pay_bg_ll);
        yonghuxieyi = (LinearLayout) view.findViewById(R.id.yonghuxieyi_ll);
        yonghuxieyi.setOnClickListener(this);
        close = (ImageView) view.findViewById(R.id.zhuanyun_close);
        close.setOnClickListener(this);
        jibie = (TextView) view.findViewById(R.id.zhuanyun_jibie);
        jianjie = (TextView) view.findViewById(R.id.zhuanyun_jianjie);
        pay_num = (TextView) view.findViewById(R.id.zhuanyun_pay_num);
        pay_btn = (TextView) view.findViewById(R.id.zhuanyun_pay_now);
        pay_btn.setOnClickListener(this);
        yaoqingma = (EditText) view.findViewById(R.id.yaoqingma);
        if (type.equals("yuzhang_rl")) {
//            linearLayout.setBackground(getResources().getDrawable(R.drawable.zrbg2x));
            linearLayout.setBackgroundResource(R.drawable.my_yzbgx);
            jibie.setText("院长");
            jianjie.setText("345个名额 · 纵享知识的力量");
            pay_num.setText("￥10000");
            yaoqingma.setVisibility(View.VISIBLE);
            jump_jibie = "yuanzhang";
        } else if (type.equals("zhuren_rl")) {
            linearLayout.setBackgroundResource(R.drawable.zrbgx);
            jibie.setText("主任");
            jianjie.setText("172个名额 · 想我所想");
            pay_num.setText("￥5000");
            yaoqingma.setVisibility(View.VISIBLE);
            jump_jibie = "zhuren";
        } else if (type.equals("daoshi_rl")) {
            linearLayout.setBackgroundResource(R.drawable.dsbgx);
            jibie.setText("导师");
            jianjie.setText("34个名额 · 优质资源随心想");
            pay_num.setText("￥1000");
            yaoqingma.setVisibility(View.VISIBLE);
            jump_jibie = "daoshi";
        } else if (type.equals("xueyuan_rl")) {
            linearLayout.setBackgroundResource(R.drawable.zrbgx);
            jibie.setText("学员");
            jianjie.setText("精彩内容全掌握");
            pay_num.setText("￥399");
            yaoqingma.setVisibility(View.VISIBLE);
            jump_jibie = "xueyuan";
        }
//        WindowManager wm = (WindowManager) getContext()
//                .getSystemService(Context.WINDOW_SERVICE);
//
//        int width = wm.getDefaultDisplay().getWidth();
//        int height = wm.getDefaultDisplay().getHeight();
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        payPopwindow = new PopupWindow(view, width - 40, height/2, true);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f; //0.0-1.0
//        getWindow().setAttributes(lp);
//        ColorDrawable dw = new ColorDrawable(0x60000000);
        payPopwindow.setBackgroundDrawable(new ColorDrawable());
        payPopwindow.setFocusable(true);
        payPopwindow.setOutsideTouchable(true);
        payPopwindow.showAtLocation(jibie_pay, Gravity.CENTER, 0, 0);
    }
}
